#import "FFMpeg.h"
#import <mobileffmpeg/MobileFFmpeg.h>

@implementation FFMpeg

- (void)exec:(CDVInvokedUrlCommand*)command {
    //https://github.com/tanersener/mobile-ffmpeg/wiki/IOS
    NSString* cmd = [[command arguments] objectAtIndex:0];
    NSString* responseToUser;
    NSString* responseStatus;
    [MobileFFmpeg execute: cmd];

    int returnCode = [MobileFFmpeg getLastReturnCode];
    NSString *output = [MobileFFmpeg getLastCommandOutput];

    if (returnCode == RETURN_CODE_SUCCESS) {
        responseToUser = [NSString stringWithFormat: @"success out=%@", output];
        responseStatus = CDVCommandStatus_OK;
    } else if (returnCode == RETURN_CODE_CANCEL) {
        responseToUser = [NSString stringWithFormat: @"canceld"];
        responseStatus = CDVCommandStatus_OK;
    } else {
        responseToUser = [NSString stringWithFormat: @"failure code=%d out=%@", returnCode, output];
        responseStatus = CDVCommandStatus_ERROR;
    }

    CDVPluginResult* result = [CDVPluginResult
                               resultWithStatus:responseStatus
                               messageAsString:responseToUser];

    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

- (void)stop:(CDVInvokedUrlCommand*)command{
    [MobileFFmpeg cancel];
    
    CDVPluginResult* result = [CDVPluginResult
                               resultWithStatus:CDVCommandStatus_OK
                               messageAsString:nil];

    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

@end
